package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.MaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 嘉德罗斯
 * @date 2021/11/22
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper,WmMaterial> implements WmMaterialService {

    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Autowired
    private FileStorageService fileStorageService;

    @Override
    public ResponseResult find(MaterialDto dto,Integer id) {
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmMaterial::getUserId,id);
        if (dto.getIsCollection() == 1){
            queryWrapper.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        }
        IPage<WmMaterial> page = new Page(dto.getPage(),dto.getSize());
        queryWrapper.orderByDesc(WmMaterial::getCreatedTime);
        //IPage<WmMaterial> wmMaterialPage = wmMaterialMapper.selectPage(page, queryWrapper);
        ResponseResult<Object> responseResult = new ResponseResult<>();
        page = page(page,queryWrapper);
        responseResult.setCode(200);
        List<WmMaterial> records = page.getRecords();
        responseResult.setData(records);
        return responseResult;
    }

    @Override
    public ResponseResult upload(MultipartFile multipartFile, int id) throws IOException {

        UUID uuid = UUID.randomUUID();
        String url = fileStorageService.uploadImgFile("", uuid+".jpg", multipartFile.getInputStream());
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setUserId(id);
        short i = 0;
        wmMaterial.setIsCollection(i);
        wmMaterial.setType(i);
        wmMaterial.setUrl(url);
        wmMaterialMapper.insert(wmMaterial);
        ResponseResult<Object> responseResult = new ResponseResult<>();
        responseResult.setCode(200);
        return responseResult;
    }

    @Override
    public ResponseResult delete(Integer id) {
        WmMaterial wmMaterial = wmMaterialMapper.selectById(id);
        String url = wmMaterial.getUrl();
        fileStorageService.delete(url);
        wmMaterialMapper.deleteById(id);
        return ResponseResult.okResult(200,"成功删除");
    }

    @Override
    public ResponseResult collect(Integer id) {
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setId(id);
        short i = 1;
        wmMaterial.setIsCollection(i);
        wmMaterialMapper.updateById(wmMaterial);
        return ResponseResult.okResult(200,"成功收藏");
    }

    @Override
    public ResponseResult cancel(Integer id) {
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setId(id);
        short i = 0;
        wmMaterial.setIsCollection(i);
        wmMaterialMapper.updateById(wmMaterial);
        return ResponseResult.okResult(200,"成功取消收藏");
    }

    @Override
    public List<Integer> getMaterial(String[] images) {
        //String[] split = images.split(",");
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        List<Integer> mIds = new ArrayList<>();
        for (String url : images) {
            queryWrapper.eq(WmMaterial::getUrl,url);
            WmMaterial wmMaterial = wmMaterialMapper.selectOne(queryWrapper);
            mIds.add(wmMaterial.getId());
        }
        return mIds;
    }
}
